<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <link href="/assets/focusManagement.css" rel="stylesheet" type="text/css" />
    <link href="/assets/accessibility.liveRegionAttachment.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
    <style>
      .webchat__scroll-to-end-button__status {
        background-color: #cc000044;
      }
    </style>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const store = testHelpers.createStore();
        const directLine = WebChat.createDirectLine({ token: await testHelpers.token.fetchDirectLineToken() });

        WebChat.renderWebChat(
          {
            directLine,
            store,
            styleOptions: { subtle: '#666', internalLiveRegionFadeAfter: 10000 }
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        await pageObjects.sendMessageViaSendBox('herocard');
        await pageConditions.minNumActivitiesShown(2);
        await pageConditions.scrollToBottomCompleted();

        // SETUP: Focus on the first button of the activity just above the "New messages" button.
        const lastButtonOnFirstCard = pageElements.activities()[1].querySelector('button');

        lastButtonOnFirstCard.focus();

        // SETUP: Scroll to bottom minus 10px.
        await pageObjects.scrollToTop(-10);
        await host.snapshot('local');

        // SETUP: Receive a new card, so the "New messages" button is shown.
        await pageObjects.sendMessageViaCode('herocard qna 1');
        await pageConditions.minNumActivitiesShown(4);

        // WHEN: Focusing inside the activity, announced hot key is pressed.
        await host.sendKeys('+ALT', '+SHIFT', 'u', '-SHIFT', '-ALT');

        // THEN: The focus should land on the "New messages" button.
        expect(document.activeElement === pageElements.scrollToEndButton()).toBe(true);

        expect(pageElements.scrollToEndButton().getAttribute('aria-keyshortcuts')).toBe('Alt Shift U');

        await host.snapshot('local');

        // WHEN: ENTER key is pressed on the "New messages" button.
        await host.sendKeys('ENTER');

        // THEN: It should scroll to the bottom.
        await pageConditions.scrollToBottomCompleted();

        // THEN: It should focus on the transcript.
        expect(document.activeElement).toBe(pageElements.transcript());

        // THEN: It should focus on the first unread activity.
        expect(pageElements.focusedActivity()).toBe(pageElements.activities()[2]);

        await host.snapshot('local');
      });
    </script>
  </body>
</html>
